package com.jztchat.webSocket.config;

import com.corundumstudio.socketio.AuthorizationListener;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.HandshakeData;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Bean;

/**
 * Created by zzy on 2018/1/18.
 */
@org.springframework.context.annotation.Configuration
public class SocketIOAutoConfig {

    @Value("${ws.server.host}")
    private String host;

    @Value("${ws.server.port}")
    private Integer port;

    @Bean
    public SocketIOServer socketIOServer(){
        Configuration config=new Configuration();
        config.setHostname(host);
        config.setPort(port);
        config.getSocketConfig().setReuseAddress(true);

        //身份验证
        config.setAuthorizationListener(new AuthorizationListener() {
            @Override
            public boolean isAuthorized(HandshakeData data) {
                return true;
            }
        });
        return new SocketIOServer(config);
    }

    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
        return new SpringAnnotationScanner(socketServer);
    }

    /**
     * 此方法作用
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(SocketIOAutoConfig.class, args);
    }
}
